/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kaaction.common.config.Global;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.Attribute;
import com.kaaction.modules.mer.entity.MerClassify;
import com.kaaction.modules.mer.service.AttributeService;
import com.kaaction.modules.mer.service.MerClassifyService;

/**
 * 分类Controller
 * @author huanghaiming
 * @version 2017-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/merClassify")
public class MerClassifyController extends BaseController {

	@Autowired
	private MerClassifyService merClassifyService;
	
	@Autowired
	private AttributeService attributeService;
	
	@ModelAttribute
	public MerClassify get(@RequestParam(required=false) String id,String classifyId) {
		MerClassify entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = merClassifyService.get(id);
		}
		if (entity == null){
			entity = new MerClassify();
		}
		
		if(StringUtils.isNotBlank(classifyId)){
			//logger.info("list:" +attributeService.getAttributeList(classifyId));
			entity.setEventAttList(attributeService.getAttributeList(classifyId));
		}
		
		return entity;
	}
	
	@RequiresPermissions("mer:merClassify:view")
	@RequestMapping(value = {"list", ""})
	public String list(MerClassify merClassify, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<MerClassify> list = merClassifyService.findList(merClassify); 
		model.addAttribute("list", list);
		return "modules/mer/merClassifyList";
	}
	
	@RequiresPermissions("mer:merClassify:view")
	@RequestMapping(value = "productrClassify")
	public String productrClassify(MerClassify merClassify, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<MerClassify> list = merClassifyService.findList(merClassify); 
		model.addAttribute("list", list);
		return "modules/mer/productClassify";
	}

	@RequiresPermissions("mer:merClassify:view")
	@RequestMapping(value = "form")
	public String form(MerClassify merClassify, Model model) {
		if (merClassify.getParent()!=null && StringUtils.isNotBlank(merClassify.getParent().getId())){
			merClassify.setParent(merClassifyService.get(merClassify.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(merClassify.getId())){
				MerClassify merClassifyChild = new MerClassify();
				merClassifyChild.setParent(new MerClassify(merClassify.getParent().getId()));
				List<MerClassify> list = merClassifyService.findList(merClassify); 
				if (list.size() > 0){
					merClassify.setSort(list.get(list.size()-1).getSort());
					if (merClassify.getSort() != null){
						merClassify.setSort(merClassify.getSort() + 30);
					}
				}
			}
		}
		if (merClassify.getSort() == null){
			merClassify.setSort(30);
		}
		model.addAttribute("merClassify", merClassify);
		return "modules/mer/merClassifyForm";
	}

	@RequiresPermissions("mer:merClassify:edit")
	@RequestMapping(value = "save")
	public String save(MerClassify merClassify, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, merClassify)){
			return form(merClassify, model);
		}
		merClassifyService.save(merClassify);
		addMessage(redirectAttributes, "保存分类成功");
		return "redirect:"+Global.getAdminPath()+"/mer/merClassify/?repage";
	}
	
	@RequiresPermissions("mer:merClassify:edit")
	@RequestMapping(value = "delete")
	public String delete(MerClassify merClassify, RedirectAttributes redirectAttributes) {
		merClassifyService.delete(merClassify);
		addMessage(redirectAttributes, "删除分类成功");
		return "redirect:"+Global.getAdminPath()+"/mer/merClassify/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<MerClassify> list = merClassifyService.findList(new MerClassify());
		for (int i=0; i<list.size(); i++){
			MerClassify e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	@RequiresPermissions("mer:merClassify:view")
	@RequestMapping(value ="attributeForm")
	public String attributeForm(MerClassify merClassify,Model model) {
		
		HashMap<String,Object> map=null;
		List<Attribute> listA = merClassify.getEventAttList();
		for(Attribute attr:listA){
			map = new HashMap<String,Object>();
			map.put("id",attr.getId());
			map.put("name",attr.getName());
			map.put("isSale",attr.getIsSale());
			map.put("name",attr.getName());
			map.put("classifyId",attr.getClassifyId());
			map.put("delFlag",attr.getDelFlag());
			map.put("sort",attr.getSort());
			merClassify.getList().add(map);
		}
		model.addAttribute("merClassify", merClassify);
		return "modules/mer/attributeForm";
	}
	
	@RequiresPermissions("mer:merClassify:edit")
	@RequestMapping(value = "saveAttribute")
	public String saveAttribute(MerClassify merClassify, Model model, RedirectAttributes redirectAttributes) {
	
		attributeService.save(merClassify);
		addMessage(redirectAttributes, "保存分类成功");
		return "redirect:"+Global.getAdminPath()+"/mer/merClassify/?repage";
	}
	
}